Skip to content

Prevent stale pet snapshots from overwriting battle progress - #85

Open
suryaswain612 wants to merge 10 commits into
mainfrom
fix/battle-accept-stale-snapshot
Open

suryaswain612 wants to merge 10 commits into
mainfrom
fix/battle-accept-stale-snapshot

Conversation

@suryaswain612

Copy link
Copy Markdown
Contributor

Description

Accept read pet progress before openBattle took the pet locks. A pet still finishing another battle could be snapshotted with old progress, skip that battle's cooldown, and later overwrite its result.

  • openBattle now rechecks the frozen progress after taking the locks, with a guarded updateMany per pet in lock order. A plain read would return the transaction's old snapshot, so it could still commit stale data.
  • A changed pet aborts the transaction. Accept reports it as the existing 409 pet-locked.
  • A serialization failure (P2034) is retried up to 3 times instead of returning a 500.

Not included: a refused accept still spends one of the defender's daily uses (M2), and the verifier does not yet check per-pet receipt continuity.

Related Issue(s)

#80

Type of Change

  • Bug fix

Component(s)

  • Backend

How Has This Been Tested?

  • pnpm --filter backend lint
  • pnpm --filter backend test (1,114 tests pass)
  • pnpm --filter backend exec tsc --noEmit

Also ran the real openBattle against a local Postgres. Without the recheck, a battle opened on stale progress, both when progress changed before open and when another battle signed during the accept. With this PR, both are refused cleanly. Concurrent accepts (20 rounds each, 80 calls) always had exactly one winner and no 500s.

Checklist

  • My code follows the style and conventions of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing tests pass locally

@suryaswain612 suryaswain612 self-assigned this Oct 22, 2026
@suryaswain612 suryaswain612 added bug Something isn't working backend labels Oct 22, 2026
@vercel

vercel Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
do-not-stop-backend Ready Ready Preview Oct 23, 2026 12:33am UTC
do-not-stop-frontend Ready Ready Preview Oct 23, 2026 12:33am UTC
do-not-stop-image-generator Ready Ready Preview Oct 23, 2026 12:33am UTC
do-not-stop-indexer-go Ready Ready Preview Oct 23, 2026 12:33am UTC
do-not-stop-website Ready Ready Preview Oct 23, 2026 12:33am UTC
1 Skipped Deployment
Project Deployment Actions Updated
do-not-stop-mobile Skipped Skipped Oct 23, 2026 12:33am UTC

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

🧪 Coverage

Package Statements Branches Functions Lines Overall
backend 84.14% (2797/3324) 78.45% (1471/1875) 82.60% (527/638) 84.57% (2649/3132) 83.00% (7444/8969)
frontend 70.17% (1654/2357) 71.37% (1441/2019) 69.10% (434/628) 71.01% (1509/2125) 70.67% (5038/7129)
shared 61.89% (1827/2952) 62.98% (1181/1875) 62.08% (388/625) 63.06% (1646/2610) 62.54% (5042/8062)

@suryaswain612 suryaswain612 linked an issue Oct 22, 2026 that may be closed by this pull request
5 tasks done
@suryaswain612
suryaswain612 marked this pull request as ready for review October 22, 2026 12:18

This branch was successfully deployed

5 active and 1 inactive deployments
Preview – do-not-stop-website — c9d9d0c3 Deployed Oct 23, 2026 by vercel[bot]
Preview – do-not-stop-frontend — c9d9d0c3 Deployed Oct 23, 2026 by vercel[bot]
Preview – do-not-stop-indexer-go — c9d9d0c3 Deployed Oct 23, 2026 by vercel[bot]
Preview – do-not-stop-backend — c9d9d0c3 Deployed Oct 23, 2026 by vercel[bot]
Preview – do-not-stop-image-generator — c9d9d0c3 Deployed Oct 23, 2026 by vercel[bot]
Preview – do-not-stop-mobile — c9d9d0c3 Deployed Oct 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent overlapping battles from overwriting pet progress

2 participants